package com.lldpet.spring.cloud.demo.order.client.event;

import org.springframework.cloud.bus.BusProperties;
import org.springframework.cloud.bus.event.AckRemoteApplicationEvent;
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
import org.springframework.cloud.bus.event.SentApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;

import java.io.Serializable;

/**
 * @desc：订单创建事件
 * @author: guanliang.xue
 * @date: 2021/5/8 18:33
 * @version:
 * @since:
 */
public class OrderCreateSuccessEvent extends RemoteApplicationEvent implements Serializable {

    /**
     * 订单编号
     */
    private String orderNo;

    /**
     * 用户Id
     */
    private String userId;

    public OrderCreateSuccessEvent() {
    }

    public OrderCreateSuccessEvent(String orderNo, String userId) {
        this.orderNo = orderNo;
        this.userId = userId;
    }

    public OrderCreateSuccessEvent(Object source, String originService, String destinationService, String orderNo, String userId) {
        super(source, originService, destinationService);
        this.orderNo = orderNo;
        this.userId = userId;
    }

    public OrderCreateSuccessEvent(Object source, String originService, String orderNo, String userId) {
        super(source, originService);
        this.orderNo = orderNo;
        this.userId = userId;
    }

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Override
    public String toString() {
        return "OrderCreateSuccessEvent{" +
                "orderNo='" + orderNo + '\'' +
                ", userId='" + userId + '\'' +
                '}';
    }

    public void ack(BusProperties bus, ApplicationEventPublisher applicationEventPublisher, Object source){
        if (bus.getTrace().isEnabled()) {
            System.out.println("applicationEventPublisher: " + applicationEventPublisher);
            AckRemoteApplicationEvent ack = new AckRemoteApplicationEvent(source,
                    this.getOriginService(),
                    bus.getAck().getDestinationService(),
                    this.getDestinationService(), this.getId(), this.getClass());
            applicationEventPublisher.publishEvent(ack);
        }
    }

}
